package com.gm.wholesale.module.main.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import com.gm.commonlib.base.baseAdapter.BaseCommAdapter
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment
import com.gm.wholesale.R
import com.gm.wholesale.constant.LeftMenuType
import com.gm.wholesale.constant.MenuData
import com.gm.wholesale.module.config.ConfigData
import com.gm.wholesale.module.main.view.MenuItemView

/**
 * 主界面侧滑 菜单view
 */
class LeftMenuFragment : BaseFragment() {
    var listener: OnMenuItemClickListener? = null

    interface OnMenuItemClickListener {
        fun onMenuItemClickListener(menuData: MenuData?)
    }

    fun setonMenuItemClickListener(listener: OnMenuItemClickListener) {
        this.listener = listener;
    }

    override fun onCreateContentBody(parent: ViewGroup) {
        val bodyView = LayoutInflater.from(activity).inflate(R.layout.view_menu_body, parent)
        bodyView.findViewById<View>(R.id.settingLin).setOnClickListener { _: View? ->
            if (listener != null) {
                listener!!.onMenuItemClickListener(MenuData.SETTING)
            }
        }
        val gridView = bodyView.findViewById<GridView>(R.id.gridView)
        gridView.adapter = object : BaseCommAdapter<MenuData?>(activity, getMenuData()) {
            override fun getView(data: MenuData?, convertView: View?, postion: Int): View {
                val menuItemView = MenuItemView(activity!!)
                menuItemView.showMenu(data!!.image, data.getName())
                menuItemView.setOnClickListener { _: View? ->
                    if (listener != null) {
                        listener!!.onMenuItemClickListener(data)
                    }
                }
                return menuItemView
            }
        }
    }

    fun getMenuData(): List<MenuData> {
        val enumList = listOf(*MenuData.values())
        val newData: MutableList<MenuData> = ArrayList()
        for (menuData in enumList) {
            if (menuData.type == LeftMenuType.RECEIV_MONEY) {
                if (!ConfigData.getConfigData().hasReceivablePermission()) {
                    continue
                }
            }
            if (menuData.type == LeftMenuType.SALES_REPORT) {
                if (!ConfigData.getConfigData().hasDataReportPermission()) {
                    continue
                }
            }
            newData.add(menuData)
        }
        return newData
    }

}